﻿
using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace GPS.Controllers.GPS_BaoCao
{
    public class GPS_CB_HocVienController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }
        public void initialCategorySearchAction(ViewReportModelGPS_CANHBAO_HOCVIEN SearchModel)
        {
            string DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            ViewBag.checkuser = DVGTVT_MA;
            string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, SearchModel.pDVGTVT_MA);
            SearchModel.pDVGTVT_MA = getdvgtvt_Ma;
            ViewBag.Messenger = string.Empty;
        }

        #region Index
        public ActionResult Index(ViewReportModelGPS_CANHBAO_HOCVIEN SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            try
            {
                var model = new List<RepostModelGPS_CANHBAO_HOCVIEN>();
                if (!string.IsNullOrEmpty(SearchModel.pDVGTVT_MA) && SearchModel.btnExportExcel == "Search")
                {
                    int icount = 1;
                    model = Getdata(SearchModel);
                    foreach (var currentEntity in model)
                    {
                        currentEntity.STT = icount.ToString();
                        icount++;
                    }
                    ViewBag.TotalRow = model.Count();
                }
                

                if (SearchModel.btnExportExcel == "ExportExcel")
                {
                    int icount = 1;
                    model = Getdata(SearchModel);
                    foreach (var currentEntity in model)
                    {
                        currentEntity.STT = icount.ToString();
                        icount++;
                    }
                    ViewBag.TotalRow = model.Count();

                    FileInfo template = new FileInfo(Server.MapPath(@"\Templates\GPS_Excel\CB_HocVienVang.xlsx"));
                    FileInfo newFile = new FileInfo(Server.MapPath(@"\Templates\GPS_Excel\CB_DonViHetHanGiayPhepNEW.xlsx"));
                    using (OfficeOpenXml.ExcelPackage excelPackage = new OfficeOpenXml.ExcelPackage(newFile, template))
                    {
                        ExcelWorkbook myWorkbook = excelPackage.Workbook;
                        ExcelWorksheet myWorksheet = myWorkbook.Worksheets["Sheet1"];
                        int recordIndex = 7;
                        foreach (var item in model)
                        {
                            if (myWorksheet.Cells[2, 1].Value == null)
                            {
                                myWorksheet.Cells[2, 1].Value = item.DVGTVT_TEN;
                            }
                            myWorksheet.Cells[recordIndex, 1].Value = item.STT.ToString();
                            myWorksheet.Cells[recordIndex, 2].Value = item.KH_TEN == null ? "" : item.KH_TEN;
                            myWorksheet.Cells[recordIndex, 3].Value = item.HOTEN == null ? "" : item.HOTEN;
                            myWorksheet.Cells[recordIndex, 4].Value = item.HV_NGAYSINH == null ? "" : item.HV_NGAYSINH;
                            myWorksheet.Cells[recordIndex, 5].Value = item.LTVANG == null ? "" : item.LTVANG;
                            myWorksheet.Cells[recordIndex, 6].Value = item.THVANG == null ? "" : item.THVANG;
                            myWorksheet.Cells[recordIndex, 7].Value = item.TDVANG == null ? "" : item.TDVANG;
                            recordIndex++;
                        }
                        excelPackage.Save();
                    }
                    string contentType = string.Empty;
                    string CurrentFileName = Server.MapPath(@"\Templates\GPS_Excel\CB_DonViHetHanGiayPhepNEW.xlsx");
                    if (CurrentFileName.Contains(".xlsx"))
                    {
                        contentType = "application/msexcel";
                    }
                    return File(CurrentFileName, contentType, string.Concat("BaoCaoPhuongTienMatTinHieu_" + DateTime.Now.ToShortDateString() + ".xlsx"));

                }
                var pageIndex = SearchModel.Page ?? 1;
                SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
                return View(SearchModel);
            }
            catch (Exception ex)
            {
                ViewBag.Messenger = ex.ToString();
                return RedirectToAction("Index");
            }
        }


        #endregion

        #region getdata
        public List<RepostModelGPS_CANHBAO_HOCVIEN> Getdata(ViewReportModelGPS_CANHBAO_HOCVIEN SearchModel)
        {
            var _pDVGTVT_MA = new OracleParameter("pDVGTVT_MA", OracleDbType.Varchar2, (SearchModel.pDVGTVT_MA == null ? DBNull.Value : (object)SearchModel.pDVGTVT_MA), ParameterDirection.Input);
            var _pRET = new OracleParameter("pRET", OracleDbType.RefCursor, ParameterDirection.Output);
            var entities = _db.DbContext.Database.SqlQuery<RepostModelGPS_CANHBAO_HOCVIEN>("BEGIN GPS_CANHBAO.GPS_CANHBAO_HOCVIEN(:pDVGTVT_MA,:pRET); end;", _pDVGTVT_MA, _pRET).ToList();
            return entities;
        }
        #endregion
    }
}